package com.dxl.lanju.service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;

import org.apache.log4j.Logger;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.dxl.common.utils.Utils;
import com.dxl.service.sysprop.SysPropCacheManager;

public class OssManager {
	private static final Logger LOG = Logger.getLogger(OssManager.class);

	private static final String endpointKey = "oss.endpoint";
	private static final String accessKeyIdKey = "oss.accessKeyId";
	private static final String accessKeySecretKey = "oss.accessKeySecret";
	private static final String bucketNameKey = "oss.bucketName";
	
	private static String endpoint;
	private static String accessKeyId;
	private static String accessKeySecret;
	private static String bucketName;
	
	private static OSSClient ossClient;
	
	public static void init() {
		endpoint = SysPropCacheManager.getPropValue(endpointKey);
	    accessKeyId = SysPropCacheManager.getPropValue(accessKeyIdKey);
	    accessKeySecret = SysPropCacheManager.getPropValue(accessKeySecretKey);
	    bucketName = SysPropCacheManager.getPropValue(bucketNameKey);
	    ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
	}
	
	public static void shutdown() {
		if (Utils.isNotNull(ossClient)) {
			ossClient.shutdown();
		}
	}
	
	public static void upload(String key, byte[] value) {
		upload(null, key, value);
	}
	
	public static void upload(String bucketName, String key, byte[] value) {
		if (!check()) {
			return;
		}
	    bucketName = Utils.isBlank(bucketName) ? OssManager.bucketName : bucketName;
	    try {
	    	ossClient.putObject(bucketName, key, new ByteArrayInputStream(value));
	    } finally {
	    }
	}
	
	public static void delete(String key) {
		delete(null, key);
	}
	
	public static void delete(String bucketName, String key) {
		if (!check()) {
			return;
		}
	    bucketName = Utils.isBlank(bucketName) ? OssManager.bucketName : bucketName;
	    try {
	    	ossClient.deleteObject(bucketName, key);
	    } finally {
	    }
	}
	
	public static byte[] download(String key) {
		return download(null, key);
	}
	
	public static byte[] download(String bucketName, String key) {
		if (!check()) {
			return null;
		}
	    bucketName = Utils.isBlank(bucketName) ? OssManager.bucketName : bucketName;
	    try {
	    	OSSObject ossObject = ossClient.getObject(bucketName, key);
	    	if (Utils.isNotNull(ossObject)) {
	    		InputStream in = null;
	    		ByteArrayOutputStream bout = null;
	    		try {
	    			in = ossObject.getObjectContent();
	    			bout = new ByteArrayOutputStream();
		    		byte[] buffer = new byte[1024];
		    		int len;
		    		while ((len = in.read(buffer)) > 0) {
		    			bout.write(buffer, 0, len);
		    		}
		    		return bout.toByteArray();
	    		} catch (Exception e) {
	    			LOG.error(e.getMessage(), e);
	    		} finally {
	    			if (Utils.isNotNull(in)) {
	    				try {
	    					in.close();
	    				} catch (Exception e) {
	    					LOG.error(e.getMessage(), e);
	    				}
	    				try {
	    					bout.close();
		    			} catch (Exception e) {
	    					LOG.error(e.getMessage(), e);
	    				}
	    			}
	    		}
	    	}
	    } finally {
	    }
	    return null;
	}
	
	public static void listObjects() {
		listObjects(null);
	}
	
	public static void listObjects(String bucketName) {
		if (!check()) {
			return;
		}
	    bucketName = Utils.isBlank(bucketName) ? OssManager.bucketName : bucketName;
	    
	    try {
	    	ObjectListing objectListing = ossClient.listObjects(bucketName);
	        List<OSSObjectSummary> objectSummary = objectListing.getObjectSummaries();
	        System.out.println("您有以下Object：");
	        for (OSSObjectSummary object : objectSummary) {
	            System.out.println("\t" + object.getKey());
	        }
	    } finally {
	    }
	}
	
	private static boolean check() {
		if (Utils.isNull(ossClient)) {
			LOG.error("没有初始化OssClient", new RuntimeException("没有初始化OssClient，请先调用OssManager.init()方法"));
			return false;
		}
		return true;
	}
}
